﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Common;

namespace DAL
{
    public class Notice
    {
        public Dat_Notice SelectNotice(int NoticeKey)
        {
            CIMDataContext db = new CIMDataContext();
            Dat_Notice notice = new Dat_Notice();
            try
            {
                notice = db.Dat_Notice.Single(n => n.NoticeKey == NoticeKey);
            }
            catch (Exception ex)
            {

            }
            return notice;
        }

        //select sent notice for paging 
        public IQueryable<Dat_Notice> SelectSendNotice(int StartRowIndex, int MaxRows, string Title, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            int UserType = Convert.ToInt32(HttpContext.Current.Session["UserType"]);
            int UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"]);

            IQueryable<Dat_Notice> Query;

            if (UserType == 1 || UserType == 2 || UserType == 3)
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        where n.Sender == UserKey
                        orderby n.SendTime descending
                        select n;
            }
            else
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        orderby n.SendTime descending
                        select n;
            }
            return Query.Skip(StartRowIndex).Take(MaxRows);
        }

        //select receive notice for paging 
        public IQueryable<Dat_Notice> SelectReceiveNotice(int StartRowIndex, int MaxRows, string Title, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            int UserType = Convert.ToInt32(HttpContext.Current.Session["UserType"]);
            int UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"]);

            IQueryable<Dat_Notice> Query;

            if (UserType == 1 || UserType == 2 || UserType == 3)
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        where n.Receiver == UserKey
                        orderby n.SendTime descending
                        select n;
            }
            else
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        orderby n.SendTime descending
                        select n;
            }
            return Query.Skip(StartRowIndex).Take(MaxRows);
        }

        public int SelectSendNoticeCount(int StartRowIndex, int MaxRows, string Title, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            int UserType = Convert.ToInt32(HttpContext.Current.Session["UserType"]);
            int UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"]);

            IQueryable<Dat_Notice> Query;

            if (UserType == 1 || UserType == 2 || UserType == 3)
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        where n.Sender == UserKey
                        orderby n.SendTime descending
                        select n;
            }
            else
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        where n.Sender == UserKey
                        orderby n.SendTime descending
                        select n;
            }
            return Query.Count();
        }

        public int SelectReceiveNoticeCount(int StartRowIndex, int MaxRows, string Title, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            int UserType = Convert.ToInt32(HttpContext.Current.Session["UserType"]);
            int UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"]);

            IQueryable<Dat_Notice> Query;

            if (UserType == 1 || UserType == 2 || UserType == 3)
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        where n.Receiver == UserKey
                        orderby n.SendTime descending
                        select n;
            }
            else
            {
                Query = from n in GetConditionQuery(Title, OneDate, TwoDate)
                        where n.Receiver == UserKey
                        orderby n.SendTime descending
                        select n;
            }
            return Query.Count();
        }

        private IQueryable<Dat_Notice> GetConditionQuery(string Title, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            var predicate = PredicateBuilder.True<Dat_Notice>();

            if (!string.IsNullOrEmpty(Title))
            {
                predicate = predicate.And(n => n.Title.Contains(Title.Trim()));
            }
            if (!string.IsNullOrEmpty(OneDate))
            {
                try
                {
                    DateTime onedate = Convert.ToDateTime(OneDate);
                    predicate = predicate.And(n => n.SendTime >= onedate);
                }
                catch (Exception ex) { }
            }
            if (!string.IsNullOrEmpty(TwoDate))
            {
                try
                {
                    DateTime twodate = Convert.ToDateTime(TwoDate.Split(' ')[0] + " 23:59:59");
                    predicate = predicate.And(n => n.SendTime <= twodate);
                }
                catch (Exception ex) { }
            }

            return db.Dat_Notice.Where(predicate);
        }

        public bool AddNotice(Dat_Notice notice)
        {
            CIMDataContext db = new CIMDataContext();
            db.Dat_Notice.InsertOnSubmit(notice);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void DelNotice(int NoticeKey)
        {
            CIMDataContext db = new CIMDataContext();
            Dat_Notice notice = db.Dat_Notice.Single(n => n.NoticeKey == NoticeKey);
            db.Dat_Notice.DeleteOnSubmit(notice);
            db.SubmitChanges();
        }

        public int GetNotReadCount()
        {
            CIMDataContext db = new CIMDataContext();
            return (from n in db.Dat_Notice
                         where n.IsRead == "否" &&
                         n.Receiver == Convert.ToInt32(HttpContext.Current.Session["UserKey"])
                         select n).Count();
        }

        public void SetRead(int NoticeKey)
        {
            CIMDataContext db = new CIMDataContext();
            try
            {
                Dat_Notice notice = db.Dat_Notice.Single(n => n.NoticeKey == NoticeKey && n.Receiver == Convert.ToInt32(HttpContext.Current.Session["UserKey"]));
                notice.IsRead = "是";
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
